{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1e715ab7-5f43-470e-9806-2e754ae1eaba",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'sklearn'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mneighbors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m KNeighborsClassifier  \n\u001b[1;32m      3\u001b[0m \u001b[38;5;66;03m# 定义数据和标签  \u001b[39;00m\n\u001b[1;32m      4\u001b[0m x \u001b[38;5;241m=\u001b[39m [[\u001b[38;5;241m0\u001b[39m], [\u001b[38;5;241m1\u001b[39m], [\u001b[38;5;241m2\u001b[39m], [\u001b[38;5;241m3\u001b[39m]]  \n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'sklearn'"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier  \n",
    "  \n",
    "# 定义数据和标签  \n",
    "x = [[0], [1], [2], [3]]  \n",
    "y = [0, 0, 1, 1]  \n",
    "  \n",
    "# 实例化k-近邻分类器（其中k=1）  \n",
    "estimator = KNeighborsClassifier(n_neighbors=1)  \n",
    "  \n",
    "# 使用fit方法进行训练  \n",
    "estimator.fit(x, y)  \n",
    "  \n",
    "# 使用predict方法进行预测  \n",
    "prediction = estimator.predict([[1]])  \n",
    "print(prediction)  # 输出预测结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4e046f8-0670-4e7e-9881-e14c1cd77ce4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
